package net.hn.hnms.biz.group.service.impl;

import net.hn.hnms.biz.group.domain.vo.SewageMineVo;
import net.hn.hnms.biz.group.service.GroupService;
import net.hn.hnms.biz.sewage.entity.SewageVo;
import net.hn.hnms.biz.sewage.mapper.SewageIndexaMapper;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.utils.StringUtils;
import net.hn.hnms.sys.common.redis.domain.SysOrganizationVo;
import net.hn.hnms.sys.common.redis.utils.RedisUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class GroupServiceImpl implements GroupService {
    /**
     * 污水总体表Mapper
     */

    @Resource
    private SewageIndexaMapper sewageIndexaMapper;

    /**
     * 获取煤矿污水处理量及复用量
     * @param companyCode
     * @param startDay
     * @param endDay
     * @return
     */
    @Override
    public List<SewageMineVo> getSewageAndReuseValueByDate(String companyCode, Date startDay, Date endDay) {
        if (startDay == null) {
            try {
                startDay = new SimpleDateFormat("yyyy-MM-dd").parse("2000-01-01");
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }
        if (endDay == null) {
            endDay = new Date();
        }
        List<SewageMineVo> voList = new ArrayList<>();
        if (StringUtils.isNotBlank(companyCode)) {
            //查询当前公司下面的所有煤矿
            //获取当前公司的所有煤矿
            SysOrganizationVo sysOrganizationVo = RedisUtils.getCacheObject(Constants.SYS_CODESUB_KEY + companyCode);

            for (SysOrganizationVo organizationVo : sysOrganizationVo.getSubCodeList()) {
                String orgCode = organizationVo.getOrgCode();
                String orgName = organizationVo.getOrgName();
                SewageMineVo sewageMineVo = new SewageMineVo();
                sewageMineVo.setMineCode(orgCode);
                sewageMineVo.setMineName(orgName);
                //获取生活污水处理量及复用量
                SewageVo domesticSewage = sewageIndexaMapper.getDomesticSewageBigDecimal(startDay, endDay, orgCode);
                BigDecimal domesticSewageDisposeValue = domesticSewage.getSewageDisposeValue();
                BigDecimal domesticSewageReuseValue = domesticSewage.getSewageReuseValue();
                sewageMineVo.setDomesticSewageDisposeValue(domesticSewageDisposeValue==null?BigDecimal.ZERO:domesticSewageDisposeValue);
                sewageMineVo.setDomesticSewageReuseValue(domesticSewageReuseValue==null?BigDecimal.ZERO:domesticSewageReuseValue);
                //获取工业污水处理量及复用量
                SewageVo industrialSewage = sewageIndexaMapper.getIndustrialSewageBigDecimal(startDay, endDay, orgCode);
                BigDecimal industrialSewageDisposeValue = industrialSewage.getSewageDisposeValue();
                BigDecimal industrialSewageReuseValue = industrialSewage.getSewageReuseValue();
                sewageMineVo.setIndustrialSewageDisposeValue(industrialSewageDisposeValue==null?BigDecimal.ZERO:industrialSewageDisposeValue);
                sewageMineVo.setIndustrialSewageReuseValue(industrialSewageReuseValue == null ? BigDecimal.ZERO : industrialSewageReuseValue);

                BigDecimal disposeSum = domesticSewageDisposeValue.add(industrialSewageDisposeValue);
                sewageMineVo.setSewageDisposeValue(disposeSum==null?BigDecimal.ZERO:disposeSum);
                BigDecimal reuseSum = domesticSewageReuseValue.add(industrialSewageReuseValue);
                sewageMineVo.setSewageReuseValue(reuseSum==null?BigDecimal.ZERO:reuseSum);
                voList.add(sewageMineVo);
            }
        } else {
            //获取集团所有煤矿
            SysOrganizationVo sysOrganizationVo = RedisUtils.getCacheObject(Constants.SYS_CODESUB_KEY + 0);
            for (SysOrganizationVo organizationVo : sysOrganizationVo.getSubCodeList()) {
                for (SysOrganizationVo vo : organizationVo.getSubCodeList()) {
                    String orgCode = vo.getOrgCode();
                    String orgName = vo.getOrgName();
                    SewageMineVo sewageMineVo = new SewageMineVo();
                    sewageMineVo.setMineCode(orgCode);
                    sewageMineVo.setMineName(orgName);
                    //获取生活污水处理量及复用量
                    SewageVo domesticSewage = sewageIndexaMapper.getDomesticSewageBigDecimal(startDay, endDay, orgCode);
                    BigDecimal domesticSewageDisposeValue = domesticSewage.getSewageDisposeValue();
                    BigDecimal domesticSewageReuseValue = domesticSewage.getSewageReuseValue();
                    sewageMineVo.setDomesticSewageDisposeValue(domesticSewageDisposeValue==null?BigDecimal.ZERO:domesticSewageDisposeValue);
                    sewageMineVo.setDomesticSewageReuseValue(domesticSewageReuseValue==null?BigDecimal.ZERO:domesticSewageReuseValue);
                    //获取工业污水处理量及复用量
                    SewageVo industrialSewage = sewageIndexaMapper.getIndustrialSewageBigDecimal(startDay, endDay, orgCode);
                    BigDecimal industrialSewageDisposeValue = industrialSewage.getSewageDisposeValue();
                    BigDecimal industrialSewageReuseValue = industrialSewage.getSewageReuseValue();
                    sewageMineVo.setIndustrialSewageDisposeValue(industrialSewageDisposeValue==null?BigDecimal.ZERO:industrialSewageDisposeValue);
                    sewageMineVo.setIndustrialSewageReuseValue(industrialSewageReuseValue == null ? BigDecimal.ZERO : industrialSewageReuseValue);

                    BigDecimal disposeSum = domesticSewageDisposeValue.add(industrialSewageDisposeValue);
                    sewageMineVo.setSewageDisposeValue(disposeSum==null?BigDecimal.ZERO:disposeSum);
                    BigDecimal reuseSum = domesticSewageReuseValue.add(industrialSewageReuseValue);
                    sewageMineVo.setSewageReuseValue(reuseSum==null?BigDecimal.ZERO:reuseSum);
                    voList.add(sewageMineVo);
                }
            }
        }
        return voList;
    }
}
